package com.isoftstone.studentregistration.config;

import cn.hutool.core.io.FileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author Administrator
 * @version v1.0
 * @date 2025/9/7 17:30
 */
//配置类，解决跨域
    @Configuration
public class WebConfiguration implements WebMvcConfigurer {
    @Value("${file.upload.path}")
    private String fileUploadPath;
    @Value("${file.upload.fileMapper}")
    private String fileMapper;
//    跨域请求处理
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET","POST","PUT","DELETE","OPTIONS")
                .allowedHeaders("*")
                .allowedMethods("*")
                .allowCredentials(true)
                .maxAge(3600);
    }
//    静态资源映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(fileMapper + "**")
                .addResourceLocations("file:" + fileUploadPath);
    }
}
